package com.ljc.controller;


import com.ljc.entity.BaseResponse;
import com.ljc.service.FileService;
import com.ljc.utils.FastDFSUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
public class FileController {
    @Resource
    private FileService fileService;
    @Resource
    private FastDFSUtil fastDFSUtil;

    /**
     * 获取文件的MD5
     */
    @PostMapping("/md5files")
    public BaseResponse<String> getFileMD5(MultipartFile file) throws Exception {
        String fileMD5 = fileService.getFileMD5(file);
        return new BaseResponse<>(fileMD5);
    }

    /**
     * 分片上传文件
     */
    @PutMapping("/file-slices")
    public BaseResponse<String> uploadFileBySlices(MultipartFile slice,
                                                   String fileMd5,
                                                   Integer sliceNo,
                                                   Integer totalSliceNo) throws Exception {
        String filePath = fileService.uploadFileBySlices(slice, fileMd5, sliceNo, totalSliceNo);
        return new BaseResponse<>(filePath);
    }

    /**
     * 文件分片
     */
    @GetMapping("/slices")
    private void slices(MultipartFile file) throws Exception {
        fastDFSUtil.convertFileToSlices(file);
    }

}
